package com.lt.lumentracks.controller;


import com.lt.lumentracks.entity.dto.post.PostAddDTO;
import com.lt.lumentracks.entity.dto.post.PostDTO;
import com.lt.lumentracks.entity.dto.post.PostReadDTO;
import com.lt.lumentracks.entity.dto.post.PostUpdateDTO;
import com.lt.lumentracks.entity.po.Post;
import com.lt.lumentracks.entity.vo.common.PageVO;
import com.lt.lumentracks.entity.vo.common.Result;
import com.lt.lumentracks.entity.vo.post.PostVO_list;
import com.lt.lumentracks.entity.vo.post.PostVO_show;
import com.lt.lumentracks.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 帖子主表 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private IPostService postService;


    // 增
    @PostMapping("/add")
    public Result<Integer> add(@RequestBody PostAddDTO addDTO) {
        return postService.addPost(addDTO);
    }


    // 删
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam Integer id) {
        return postService.delPostById(id);
    }


    // 改--修改帖子
    @PutMapping("/update")
    public Result<String> update(@RequestBody PostUpdateDTO updateDTO) {
        return postService.updatePostById(updateDTO);
    }

    // 改--修改帖子可见权限
    @PutMapping("/updatePostVisible")
    public Result<String> updatePostVisible(@RequestParam Integer id, @RequestParam Integer privacy) {
        return postService.updatePostVisible(id, privacy);
    }


    // 查
    // 查全部
    @GetMapping("/getAll")
    public Result<List<Post>> getAll() {
        return postService.getAllPost();
    }

    // 分页查全部（返回数据库中的Post表对应的实体类）
    @GetMapping("/getRawPostsByPage")
    public Result<PageVO<Post>> getRawPostsByPage(@RequestParam(defaultValue = "1") long current,
                                             @RequestParam(defaultValue = "10") long size) {
        PageVO<Post> allInPage = postService.getRawPostsByPage (current, size);
        return Result.success(allInPage);
    }

    // 分页查全部（返回帖子列表展示所需的所有属性）
    @GetMapping("/getDisplayPostsListByPage")
    public Result<PageVO<PostVO_list>> getDisplayPostsListByPage(@RequestParam(defaultValue = "1") long current,
                                                             @RequestParam(defaultValue = "10") long size) {
        PageVO<PostVO_list> allInPage = postService.getDisplayPostsListByPage(current, size);
        return Result.success(allInPage);
    }

    // 分页查，首页推送
    @GetMapping("/feed")
    public Result<PageVO<PostVO_list>> feed(Integer uid,
                                          @RequestParam(defaultValue = "1") int current,
                                          @RequestParam(defaultValue = "10") int size){
        List<PostVO_list> list = postService.feedForHomePage(uid, current, size);
        PageVO<PostVO_list> vo = new PageVO<>();
        vo.setPages((long) current);
        vo.setTotal(0L);
        vo.setList(list);
        return Result.success(vo);
    }

    // 分页查，查询我关注的用户发的帖子
    @GetMapping("/feedByMyFollowing")
    public Result<PageVO<PostVO_list>> feedByMyFollowing(@RequestParam Integer userId,
                                                        @RequestParam(defaultValue = "1") long current,
                                                        @RequestParam(defaultValue = "10") long size) {
        PageVO<PostVO_list> allInPage = postService.getDisplayPostsListByMyFollowing(userId, current, size);
        return Result.success(allInPage);
    }

    // 条件查询--根据标签查询
    @PostMapping("/getPostsListByTagIds")
    public Result<PageVO<PostVO_list>> getPostsListByTagIds_exact(@RequestBody List<Integer> tagIdList,
                                                            @RequestParam(defaultValue = "1") long current,
                                                            @RequestParam(defaultValue = "10") long size) {
        PageVO<PostVO_list> vo = postService.getPostsListByTagIds_exact(tagIdList, current, size);
        return Result.success(vo);
    }

    // 条件查询
    @PostMapping("/getByCondition")
    public Result<PageVO<Post>> getByCondition(@RequestBody PostDTO postDTO) {
        PageVO<Post> allInCondition = postService.getInCondition(postDTO);
        return Result.success(allInCondition);
    }

    // 根据id查询帖子信息
    @GetMapping("/getPostInfoById")
    public Result<PostVO_show> getPostInfoById(@RequestParam Integer id, @RequestParam(required = false) Integer userId) {
        PostVO_show pvo_s = postService.getPostInfoById(id, userId);
        if(pvo_s == null){
            return Result.error("帖子不存在！");
        }
        return Result.success(pvo_s);
    }


    // 根据标题查询帖子，从es中查询
    @GetMapping("/getByTitle")
    public Result<PageVO<PostVO_list>> getByTitle(@RequestParam String title,
                                         @RequestParam(defaultValue = "1") long current,
                                         @RequestParam(defaultValue = "10") long size){
        if(current < 1 || size < 1){
            return Result.error("查询失败，参数错误！");
        }

        // 不能查第5千条往后的
        long maxPage = (long) Math.ceil((double) 5000 / size);
        if (current > maxPage) {
            return Result.error("查询失败，超出查询限制！");
        }

        PageVO<PostVO_list> posts = postService.getByTitle(title, current, size);
        return Result.success(posts);
    }

    // 根据用户id，返回用户创作的帖子信息
    @GetMapping("/getByUserId")
    public Result<PageVO<PostVO_list>> getByUserId(@RequestParam Integer userId,
                                         @RequestParam(defaultValue = "1") long current,
                                         @RequestParam(defaultValue = "10") long size){
        PageVO<PostVO_list> vo = postService.getByUserId(userId, current, size);
        return Result.success(vo);
    }

    // 根据用户id，返回用户点赞的帖子信息
    @GetMapping("/getByUserLike")
    public Result<PageVO<PostVO_list>> getByUserLike(@RequestParam Integer userId,
                                                     @RequestParam(defaultValue = "1") long current,
                                                     @RequestParam(defaultValue = "10") long size){
        PageVO<PostVO_list> vo = postService.getByUserLike(userId, current, size);
        return Result.success(vo);
    }

    // 根据用户id，返回用户收藏的帖子信息
    @GetMapping("/getByUserCollect")
    public Result<PageVO<PostVO_list>> getByUserCollect(@RequestParam Integer userId,
                                                        @RequestParam(defaultValue = "1") long current,
                                                        @RequestParam(defaultValue = "10") long size){
        PageVO<PostVO_list> vo = postService.getByUserCollect(userId, current, size);
        return Result.success(vo);
    }


    /**
     * redis相关
     */
    // 浏览帖子
    @PostMapping("/read")
    public Result<String> read(@RequestBody PostReadDTO prDTO) {
        postService.read(prDTO);
        return Result.success();
    }

    // 点赞帖子
    @PostMapping("/like")
    public Result<String> like(@RequestParam Integer postId) {
        postService.like(postId);
        return Result.success();
    }

    // 收藏帖子
    @PostMapping("/collect")
    public Result<String> collect(@RequestParam Integer postId) {
        postService.collect(postId);
        return Result.success();
    }



    // 同步es中对应索引的文档信息（TODO: 这步应该放到后台管理，先用着吧）
    @GetMapping("/BulkDoc_save")
    public Result<String> BulkDoc_save(){
        postService.BulkDoc_save();
        return Result.success("同步成功！");
    }

}
